<template>
  <ul class="list">
    <li class="list-item" 
     v-for="item of letters" :key="item"
     @click="handelLetterClick" 
     @touchstart="handelTouchStart"
     @touchmove="handelTouchMove"
     @touchend="handelTouchEnd"
     :ref="item"
    >{{item}}</li>
  </ul>
</template>
<script>
import BScroll from "better-scroll";
export default {
  name: "alphabet",
  props:{
      allCities:Object
  },
  data(){
    return{
      touchStatus:false
    }
  },
  computed:{
    letters(){
      var letters = [];
      for (var key in this.allCities){
          letters.push(key)
      }
      return letters
    }
  },
  methods:{
    handelLetterClick(e){
      this.$emit('letterChange',e.target.innerText)
    },
    handelTouchStart(e){
      this.touchStatus = true;
    },
    handelTouchMove(e){
      if(this.touchStatus){
        var startY = this.$refs['A'][0].offsetTop;
        var touchY = e.touches[0].clientY -89;
        var index =  Math.floor((touchY-startY)/20);
        if(index>=0&&index<this.letters.length){
           this.$emit('letterChange',this.letters[index])
        }      
      }
    
    },
    handelTouchEnd(e){
      this.touchStatus = false;
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~style/public";
.list {
  position: absolute;
  right: 0;
  top: 1.78rem;
  bottom: 0rem;
  width: 0.4rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  .list-item {
    text-align: center;
    line-height: 0.4rem;    
    color: $bgcolor;
  }
}
</style>
